/*
 *  user_bsp.c
 *  source file for bsp
 *  Created on: Feb 17, 2025
 *      Author: aunyyuan
 */
 
#include "user_bsp.h"
#include <stdio.h>
#include "stm32f4xx_hal.h"
#if VERSION	== 1

const char version[12] = "Ver_1.0.0.01";
#elif VERSION	== 2
const char version[12] = "Ver_1.0.0.02";
#elif VERSION	== 3
const char version[12] = "Ver_1.0.0.03";
#elif VERSION	== 4
const char version[12] = "Ver_1.0.0.04";
#elif VERSION	== 5
const char version[12] = "Ver_1.0.0.05";
#else
const char version[12] = "Ver_1.0.0.00";	
#endif

uint16_t Get_All_Sensor(void)
{
	uint16_t data;
	uint8_t ret;
	
	ret = GET_SENSOR_01;
	if (ret != GPIO_PIN_RESET)
		data = 1;
	else
		data = 0;
	
	ret = GET_SENSOR_02;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 1);
	
	ret = GET_SENSOR_03;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 2);
	
	ret = GET_SENSOR_04;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 3);
	
	ret = GET_SENSOR_05;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 4);
	ret = GET_SENSOR_06;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 5);
	
	ret = GET_SENSOR_07;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 6);
	ret = GET_SENSOR_08;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 7);
	ret = GET_SENSOR_09;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 8);
	
	ret = GET_SENSOR_10;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 9);
	ret = GET_SENSOR_11;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 10);
	ret = GET_SENSOR_12;
	if (ret != GPIO_PIN_RESET)
		data |= (1 << 11);
	
	return data;
}
uint8_t Get_Single_Sensor(Sensor_No no)
{
    uint8_t ret;
	switch(no)
	{
		case 1:
			ret = GET_SENSOR_01;
			break;
		case 2:
			ret = GET_SENSOR_02;
			break;
		case 3:
			ret = GET_SENSOR_03;
			break;
		case 4:
			ret = GET_SENSOR_04;
			break;
		case 5:
			ret = GET_SENSOR_05;
			break;
		case 6:
			ret = GET_SENSOR_06;
			break;
		case 7:
			ret = GET_SENSOR_07;
			break;
		case 8:
			ret = GET_SENSOR_09;
			break;
		case 9:
			ret = GET_SENSOR_09;
			break;
		case 10:
			ret = GET_SENSOR_10;
			break;
		case 11:
			ret = GET_SENSOR_11;
			break;
		case 12:
			ret = GET_SENSOR_12;
			break;
	}
	return ret;
}

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
	HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_SET);
}
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
	HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET);
}
void GPIO_Toggle(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
	GPIO_PinState bitstatus;
	
	bitstatus = HAL_GPIO_ReadPin(GPIOx, GPIO_Pin);
	if (bitstatus == GPIO_PIN_RESET)
		GPIO_SetBits(GPIOx, GPIO_Pin);
	else
		GPIO_ResetBits(GPIOx, GPIO_Pin);
}


uint8_t Get_Self_Id(void)
{
	uint8_t ret, lucData;
		
	ret = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_10);
	if (ret == GPIO_PIN_SET)
		lucData = 1;
	else
		lucData = 0;
	ret = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_11);
	if (ret == GPIO_PIN_SET)
		lucData |= 2;
	ret = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12);
	if (ret == GPIO_PIN_SET)
		lucData |= 4;
	ret = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_13);
	if (ret == GPIO_PIN_SET)
		lucData |= 8;
	ret = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_14);
	if (ret == GPIO_PIN_SET)
		lucData |= 0x10;
	ret = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15);
	if (ret == GPIO_PIN_SET)
		lucData |= 0x20;
		
	return lucData;
}

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;
extern UART_HandleTypeDef huart6;
extern UART_HandleTypeDef huart4;
int fputc(int ch, FILE *f) {
    HAL_UART_Transmit(&huart4, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
    return ch;
}
//Can_Process();
//timer_test();
/***************************End of File***************************************/
